class Solution(object):
    def shortestWordDistance(self, wordsDict, word1, word2):
        """
        :type wordsDict: List[str]
        :type word1: str
        :type word2: str
        :rtype: int
        """
        if word1 == word2:
            mode = 0
        else:
            mode = 1
        w_d = {}
        min_value = 10**5
        for i,word in enumerate(wordsDict):
            if word == word1 or word == word2:
                if mode == 0:
                    if word not in w_d:
                        w_d[word] = i
                    else:
                        min_value = min(min_value, i - w_d[word])
                        w_d[word] = i
                else:
                    w_d[word] = i
                    if len(w_d) == 2:
                        min_value = min(min_value, abs(w_d[word1] - w_d[word2]))
                if min_value == 1:
                    return min_value
        return min_value


data = Solution()
wordsDict = ["practice", "makes", "perfect", "coding", "makes"]
word1 = "makes"
word2 = "coding"
# word2 = "makes"
print(data.shortestWordDistance(wordsDict, word1, word2))
